


import 'dart:async';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

/// 防止文字自动换行
extension FixAutoLines on String {
  String fixAutoLines() {
    return Characters(this).join('\u{200B}');
  }
}

extension MapExtension on Map {
  /// 反转 Map 的 key 和 value
  ///
  /// 重复 key 将改写早入的 entry
  Map reverse() {
    Map map = {};
    forEach((key, value) {
      map[value] = key;
    });
    return map;
  }

  /// 反转 Map 的 key 和 value
  ///
  /// 忽略重复的 key
  Map reverseIgnoreDuplication() {
    Map map = {};
    forEach((key, value) {
      if (!map.containsKey(value)) {
        map[value] = key;
      }
    });
    return map;
  }
}

///https://github.com/flutter/flutter/issues/59757
///官方已修复，但是不想更新版本，自己缝补下
extension StreamControllerE<T extends dynamic> on StreamController<T>{
  void safetyAdd(T){
    if(!isClosed){
      sink.add(T);
    }
  }

  void safetyError(T){
    if(!isClosed){
      sink.addError(T);
    }
  }
}

extension ResponseE on Response {
  /// 获取响应头
  List<dynamic> toList() {
    if(data is List){
      return data as List;
    }else if(data['list'] != null){
      return (data as Map)['list'];
    }else if(data['lists'] != null){
      return (data as Map)['lists'];
    }else{
      return [];
    }
  }
}